from sqlalchemy.orm import Session
from app.models.spot import Spot
from app.models.user import User
from app.core.security import get_password_hash

def seed_admin(db: Session):
    """创建管理员账号"""
    # 先检查用户名是否存在
    existing_admin = db.query(User).filter(User.username == "admin").first()
    if existing_admin:
        # 如果用户存在但不是管理员，则更新为管理员
        if existing_admin.role != "admin":
            existing_admin.role = "admin"
            db.commit()
            print("已将现有用户更新为管理员")
        else:
            print("管理员账号已存在")
        return

    # 如果用户不存在，创建新管理员
    admin = User(
        username="admin",
        email="admin@example.com",
        password=get_password_hash("admin123"),
        role="admin"
    )
    db.add(admin)
    db.commit()
    print("管理员账号创建成功!")

def seed_spots(db: Session):
    spots = [
        {
            "name": "西湖",
            "description": "西湖是位于浙江省杭州市西湖区龙井路1号的中国大陆首个世界文化遗产。景区内包括断桥残雪、平湖秋月等著名景点。",
            "location": "浙江省杭州市西湖区",
            "type": "自然景观",
            "open_time": "全天开放",
            "price": 80.00,
            "images": "/images/spots/xihu.jpg",
            "inventory": 1000,
            "rating": 4.8
        },
        {
            "name": "故宫",
            "description": "故宫是中国明清两代的皇家宫殿，位于北京中轴线的中心，是中国古代宫廷建筑之精华。",
            "location": "北京市东城区景山前街4号",
            "type": "人文景观",
            "open_time": "8:30-17:00",
            "price": 120.00,
            "images": "/images/spots/forbidden-city.jpg",
            "inventory": 800,
            "rating": 4.9
        },
        {
            "name": "长城",
            "description": "万里长城是中国古代的伟大建筑之一，也是世界文化遗产。八达岭长城是明长城中保存最好的一段。",
            "location": "北京市延庆区",
            "type": "人文景观",
            "open_time": "7:30-17:30",
            "price": 100.00,
            "images": "/images/spots/great-wall.jpg",
            "inventory": 2000,
            "rating": 4.7
        }
    ]

    for spot_data in spots:
        spot = db.query(Spot).filter(Spot.name == spot_data["name"]).first()
        if not spot:
            spot = Spot(**spot_data)
            db.add(spot)
    
    db.commit() 